package com.dg11185.nearshop.activity.user.order;

import android.os.Bundle;
import android.view.View;

import com.android.nearshop.R;
import com.dg11185.libs.network.NetClient;
import com.dg11185.libs.utils.date.DateUtils;
import com.dg11185.nearshop.activity.SubPageActivity;
import com.dg11185.nearshop.net.CacheConstant;
import com.dg11185.nearshop.net.NetCacheClient;
import com.dg11185.nearshop.net.request.MyActivityRecordListRequest;
import com.dg11185.nearshop.net.response.MyActivityRecordListResponse;
import com.dg11185.nearshop.net.response.NetStatus;
import com.dg11185.nearshop.view.adapter.LotteryRecordAdapter;
import com.dg11185.nearshop.view.xlistview.XListView;
import com.dg11185.nearshop.view.xlistview.XListView.IXListViewListener;
import com.wlper.lib.view.loading.BaseLoadLayout.LoadStateHandler;
import com.wlper.lib.view.loading.LoadLayoutWithTweenAnim;

public class LotteryRecordActivity extends SubPageActivity {

    private static final String USER_ID = "1";

    private XListView listView = null;

    private LotteryRecordAdapter listaAdapter = null;

    private LoadLayoutWithTweenAnim loadingView = null;

    private MyActivityRecordListRequest request = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_xlistview);
        setSubPageTitle("抽奖单");

        listView = (XListView) this.findViewById(R.id.XListView);
        listView.setPullLoadEnable(false);
        listView.setXListViewListener(pullListener);
        listView.setVisibility(View.GONE);
        listaAdapter = new LotteryRecordAdapter(this);
        listView.setAdapter(listaAdapter);

        loadingView = (LoadLayoutWithTweenAnim) this.findViewById(R.id.common_loading_layout);
        loadingView.setStateHandler(stateHandler);
        loadingView.setTouchEventValid(true);

        request = new MyActivityRecordListRequest(USER_ID).encryption();
        request.setActionListener(listener);
        NetCacheClient.httpCacheGet(request);
    }

    @Override
    protected void onDestroy() {
        NetClient.httpCancel(this, true);
        super.onDestroy();
    }

    private MyActivityRecordListRequest.ActionListener<MyActivityRecordListResponse> listener = new MyActivityRecordListRequest.ActionListener<MyActivityRecordListResponse>() {

        @Override
        public void onSuccess(MyActivityRecordListResponse result) {
            if (result.status == NetStatus.SUCCESS) {
                if (result.records.size() != 0) {
                    loadingView.onLoadSuccess();
                    listView.stopRefresh();
                    listView.setRefreshTime(DateUtils.getCurrentTime());
                    listView.setVisibility(View.VISIBLE);
                    listaAdapter.setRecords(result.records);
                    listaAdapter.notifyDataSetChanged();
                } else {
                    // TODO 数据为空
                    onFailure(-2);
                }
            } else {
                onFailure(-1);
            }
        }

        @Override
        public void onFailure(int resultCode) {
            loadingView.onLoadFailed();
            listView.stopRefresh();
            if (resultCode == NetClient.NET_DISCONNECT_CODE) {
                loadingView.setLoadFailedHint(R.string.cant_connect_network);
            } else if (resultCode == NetClient.NET_DISCONNECT_CODE) {
                loadingView.setLoadFailedHint("你还没有抽奖数据");
            } else {
                loadingView.setLoadFailedHint(R.string.network_error);
            }
        }

    };

    private LoadStateHandler stateHandler = new LoadStateHandler() {

        @Override
        public void onReload() {
            NetCacheClient.httpCacheGet(request);
        }

        @Override
        public void onLoading() {

        }

        @Override
        public void onLoadSuccess() {

        }

        @Override
        public void onLoadFailed() {

        }
    };

    private IXListViewListener pullListener = new IXListViewListener() {

        @Override
        public void onRefresh() {
            CacheConstant.deleteCacheFile(request);
            NetClient.httpGet(request);
        }

        @Override
        public void onLoadMore() {
        }
    };
}
